package org.wenhua.yuan.and.fragment

import  android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_cmbc_tab.*
import org.wenhua.androidaar.JHDisplayUtils
import org.wenhua.preloadlib.activity.ActivityPreloadManager

import org.wenhua.yuan.R
import org.wenhua.yuan.and.cmbc.*
import org.wenhua.yuan.and.demo.MainBasePreloadActivity
import org.wenhua.yuan.uilib.UIAdapter
import org.wenhua.yuan.uilib.UIDataItem
import java.util.*

class CMBCTabFragment : IBaseFragment() {
    val dataItems:MutableList<UIDataItem<*, RecyclerView.ViewHolder>> = ArrayList<UIDataItem<*, RecyclerView.ViewHolder>>()
    override fun getLayoutRes(): Int {
        return R.layout.fragment_cmbc_tab
    }

    @SuppressLint("FragmentLiveDataObserve")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        val aar = JHDisplayUtils.dp2px(18f)
//        val jar = JHJarDisplayUtils.dp2px(20)
//        aar_jar_show.text = "aar包引入后显示：${aar} - jar包引入后显示：${jar}"

        val uiAdapter = UIAdapter(requireContext())
        cmbcRecyclerView.layoutManager = LinearLayoutManager(context)
        cmbcRecyclerView.adapter = uiAdapter

        val cmbcDataMenuAreaItems: List<CMBCDataMenuArea> = ArrayList<CMBCDataMenuArea>()
        dataItems.add(CMBCMenuAreaItem(cmbcDataMenuAreaItems))

        val cmbcDataFeatureAreaItems: List<CMBCDataFeatureArea> = ArrayList<CMBCDataFeatureArea>()
        dataItems.add(CMBCFeatureAreaItem(cmbcDataFeatureAreaItems))

        val cmbcDataMiddleBannerAreaItems: List<CMBCDataMiddleBannerArea> = ArrayList<CMBCDataMiddleBannerArea>()
        dataItems.add(CMBCBannerAreaItem(cmbcDataMiddleBannerAreaItems, requireContext()))


        val cmbcDataFinanceAreaItems: List<CMBCDataFinanceArea> = ArrayList<CMBCDataFinanceArea>()
        dataItems.add(CMBCFinanceAreaItem(cmbcDataFinanceAreaItems))

        val cmbcDataFinance2AreaItems: List<CMBCDataFinanceArea> = ArrayList<CMBCDataFinanceArea>()
        dataItems.add(CMBCFinance2AreaItem(cmbcDataFinance2AreaItems))

        uiAdapter.addItems(dataItems, false)


        initPreloadView()

    }

    private fun initPreloadView() {

        val intent: Intent = Intent(this.activity, MainBasePreloadActivity::class.java)
        ActivityPreloadManager.preload(intent, this.requireContext(), R.layout.activity_main_preload) // 预加载View


        val rootView:FrameLayout = requireActivity().findViewById<View>(android.R.id.content) as FrameLayout

        val textView:TextView = TextView(requireActivity())
        val param:FrameLayout.LayoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)
        param.gravity = Gravity.BOTTOM or Gravity.RIGHT
        param.bottomMargin = JHDisplayUtils.dp2px(100f)
        textView.layoutParams = param
        textView.text = "跳转到预加载Activity"
        textView.tag = "text-view"
        textView.setBackgroundColor(Color.parseColor("#000000"))
        textView.setTextColor(Color.parseColor("#ffffff"))
        textView.setPadding(10, 5, 10,5)
        textView.setOnClickListener {
            startActivity(intent)

        }
        rootView.addView(textView)

    }

}

